package co.id.vivastor.pantau.util;

import co.id.vivastor.pantau.MainActivity;
import co.id.vivastor.pantau.SettingActivity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class Global {
	public static final String KODE_WILAYAH = "kodeWilayah";
	public static final String NOMOR_TPS = "nomorTPS";	
	public static final String SMS_CENTER_NO = "SMSCenterNumber";
	public static final String DEFAULT_VALUE = "0";
	
	public static void saveData(Context con, String variable, String data) {
	    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(con);
	    prefs.edit().putString(variable, data).commit();
	}
	
	public static String getData(Context con, String variable, String defaultValue) {
	    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(con);
	    String data = prefs.getString(variable, defaultValue);
	    return data;
	}
	
	public static void showDialog(final Context context, String title, String message, String buttonText) {
	    AlertDialog.Builder builder = new AlertDialog.Builder(context);
	    builder.setTitle(title);
	    builder.setMessage(message);
	    builder.setPositiveButton(buttonText, new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				
				Intent intent = new Intent(context, SettingActivity.class);
				context.startActivity(intent);
			}
		});
	    builder.setCancelable(false);
	    builder.create().show();
	}
	
	public static void setImageViewButtonListener(ImageView menu, final Context context, final Class<?> nextActivityClass) {
		menu.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
	                case MotionEvent.ACTION_DOWN: {
	                    ImageView view = (ImageView) v;
	                    view.getDrawable().setAlpha(100);
	                    view.invalidate();
	                    break;
	                }
	                case MotionEvent.ACTION_UP: {
	                    ImageView view = (ImageView) v;
	                    view.getDrawable().setAlpha(255);
	                    view.invalidate();
	                    break;
	                }
	            }

            return false;
			}
		});
		menu.setOnClickListener(new View.OnClickListener(){
		    public void onClick(View v) {
		    	Intent intent = new Intent(context, nextActivityClass);
				context.startActivity(intent);
		    }
		});
	}
}
